﻿@{
    Layout = null;
}
@using KingRoad.EPM.Common.Helper
<script type="text/javascript">

    //树
    var zNodes_permission_workgroup_index = [];
    var zTree_permission_workgroup_index;
    var setting_permission_workgroup_index;
    var deleteID_permission_workgroup_index;
    var rootNodeId_permission_workgroup_index = "0"; //记录根节点id
    var height_permission_workgroup_index=0;//高度
    //全局的href
    var href_permission_workgroup_index = "/Permission/Workgroup";
    setting_permission_workgroup_index = {
        checkable: false,
        async: true,
        isSimpleData: true,
        editable: true,
        edit_renameBtn: false,
        edit_removeBtn: false,
        dragCopy: false,
        dragMove: false,
        asyncUrl: "/Permission/Workgroup/GetWorkgroupNodes",  //获取节点数据的URL地址
        //asyncDataFilter: ajaxDataFilter,
        nameCol: "Name",
        open: false,
        treeNodeKey: "Id",
        treeNodeParentKey: "ParentId",
        callback: {
            //             beforeAsync: zTreeBeforeAsync,
            asyncSuccess: zTreeOnAsyncSuccess_permission_workgroup_index,
            //             asyncError: zTreeOnAsyncError,
            click: zTreeOnClick_permission_workgroup_index
        }
    };

    function zTreeOnAsyncSuccess_permission_workgroup_index(event, treeId, treeNode, msg) {
        //异步成功
    }
    //树的单击事件
    function zTreeOnClick_permission_workgroup_index(event, treeId, treeNode) {
        //添加工作组
        var addWorkgroupUrl_permission_workgroup_index = href_permission_workgroup_index + "/CreateWorkgroup/" + treeNode.Id;
        $("#addWorkgroup_permission_workgroup_index").attr("href", addWorkgroupUrl_permission_workgroup_index);
        $("#removeStaff_permission_workgroup_index").hide();
        if (treeNode.Id != 0) {//点的不是根节点
            deleteID_permission_workgroup_index = treeNode.Id; //点击树节点就记录可能删除的节点id
            //更新岗位
            var editWorkgroupUrl_permission_workgroup_index = href_permission_workgroup_index + "/EditWorkgroup/" + treeNode.Id;
            $("#editWorkgroup_permission_workgroup_index").attr("href", editWorkgroupUrl_permission_workgroup_index);

            //刷新该工作组内的所有职员
            $("#list_permission_workgroup_index").trigger("reloadGrid");
            //为加入职员按钮赋值
            $("#addStaff_permission_workgroup_index").attr("href", "/Permission/Workgroup/SelectStaff/" + treeNode.Id);
            $("#addStaff_permission_workgroup_index").show();
        }
        else {
            $("#addStaff_permission_workgroup_index").hide();
        }

    }

    $(document).ready(function () {
        $("#addStaff_permission_workgroup_index").hide();
        $("#removeStaff_permission_workgroup_index").hide();
        height_permission_workgroup_index = $("#tabContent").innerHeight() - $(".panelBar").height();
    });

    function refreshTree(asyncUrl) {
        $("#getUrl").html("");
        $("#onAsyncSuccessNode").html("<br/>");
        $("#onAsyncErrorNode").html("<br/>");
        setting_permission_workgroup_index.asyncUrl = asyncUrl;
        if ((typeof asyncUrl) == "function") {
            $("#asyncUrlCode").html("setAsyncUrl");
        } else {
            $("#asyncUrlCode").html("\"" + asyncUrl + "\"");
        }
        zTree_permission_workgroup_index = $("#treeWorkgroup_permission_workgroup_index").zTree(setting_permission_workgroup_index, zNodes_permission_workgroup_index);
    }
    function dialogAdded_Permission_Workgroup(json) {
        //提交回调
        //1 接收参数 2 判断成功与否 3 根据返回的数据在树上新增节点
        DWZ.ajaxDone(json);
        if (json.statusCode == DWZ.statusCode.ok) {
            //添加成功
            if (json.callbackType == "Create") {
                var newParentNode = zTree_permission_workgroup_index.getNodeByParam("Id", json.data.ParentId)
                zTree_permission_workgroup_index.addNodes(newParentNode, json.data);
                var newNode = zTree_permission_workgroup_index.getNodeByParam("Id", json.data.Id)
                zTree_permission_workgroup_index.selectNode(newNode);
                //更新选中职员
                deleteID_permission_workgroup_index = newNode.Id; //点击树节点就记录可能删除的节点id
                //更新岗位
                var editWorkgroupUrl_permission_workgroup_index = href_permission_workgroup_index + "/EditWorkgroup/" + newNode.Id;
                $("#editWorkgroup_permission_workgroup_index").attr("href", editWorkgroupUrl_permission_workgroup_index);
                //刷新该工作组内的所有职员
                $("#list_permission_workgroup_index").trigger("reloadGrid");
                //为加入职员按钮赋值
                $("#addStaff_permission_workgroup_index").attr("href", "/Permission/Workgroup/SelectStaff/" + newNode.Id);
            } else if (json.callbackType == "Edit") {
                //更新成功
                var selectedNode = zTree_permission_workgroup_index.getSelectedNode();
                selectedNode.Name = json.data.Name;
                zTree_permission_workgroup_index.updateNode(selectedNode, true);
            }
            $.pdialog.closeCurrent();
        }
    }
    //工作组删除回调函数
    function delworkgroup_permission_workgroup_index(json) {
        DWZ.ajaxDone(json);
        if (json.statusCode == DWZ.statusCode.ok) {
            //当前删除节点清除
            var selectedNode = zTree_permission_workgroup_index.getSelectedNode();
            zTree_permission_workgroup_index.removeNode(selectedNode, true);
            //当前已选ID清空
            deleteID_permission_workgroup_index = null;
            //当前详情清空
            $("#list_permission_workgroup_index").clearGridData();
        }
    }

    function StaffAdded_Permission_Workgroup(json) {
        //加入职员成功后，刷新当前列表
        DWZ.ajaxDone(json);
        if (json.statusCode == DWZ.statusCode.ok) {
            $.pdialog.closeCurrent();
            $("#list_permission_workgroup_index").trigger("reloadGrid");
        }
    }

    //*****构造职员树
    function InitGrid_Permission_Workgroup() {
        $("#list_permission_workgroup_index").jqGrid({
            url: "/Permission/Workgroup/GetStaffsByWorkgroupId",
            datatype: "json",
            mtype: "post",
            colNames: ['Id', '用户名', '编号', '姓名', '描述'],
            colModel: [
                            { label: 'Id', name: 'Id', index: 'Id', width: 5, align: "center", sortable: false, hidden: true },
                            { label: '用户名', name: 'UserName', index: 'UserName', width: 120, align: "center", sortable: false },
                            { label: '编号', name: 'Code', index: 'Code', width: 100, align: "center", sortable: false },
                            { label: '姓名', name: 'RealName', index: 'RealName', width: 100, align: "center", sortable: false },
                            { label: '描述', name: 'Description', index: 'Description', width: 130, align: "center", sortable: false }
                           ],
            beforeRequest: function (d) {
                var selectedNode = zTree_permission_workgroup_index.getSelectedNode();
                if (selectedNode!=null&&selectedNode.Id != 0)
                    $(this).jqGrid('setGridParam', { postData: { "WorkgroupId": selectedNode.Id} });
            },
            loadError: function (xhr, status) {
                alertMsg.error(xhr.responseText);
            },
            loadComplete: function (xhr) {
                var ajaxbg = $("#background,#progressBar,#lui_list_permission_workgroup_index");
                ajaxbg.hide();
            },
            onSelectRow: SelectStaff_Permission_Workgroup,
            autowidth: true,
            shrinkToFit: true,
            multiselect: false,
            multiselectWidth: 50,
            sortable: false,
            viewrecords: true,
            sortorder: "desc",
            jsonReader: {
                repeatitems: false
            },
            height: height_permission_workgroup_index
        }).navGrid('#pager_permission_workgroup_index', { edit: false, add: false, del: false, refresh: false, search: false });
    }

    function SelectStaff_Permission_Workgroup() {
        //选择职员，并可移除该职员
        $("#removeStaff_permission_workgroup_index").show();
    }


    function StaffRemoved_Permission_Workgroup(json) {
        DWZ.ajaxDone(json);
        if (json.statusCode == DWZ.statusCode.ok) {
            $("#list_permission_workgroup_index").trigger("reloadGrid");
        }
    }

    //****************
    $(function () {
        refreshTree("/Permission/Workgroup/GetWorkgroupNodes");
        $("#del_permission_workgroup_index").click(function () {
            //删除工作组
            if (zTree_permission_workgroup_index.getSelectedNode()) {
                var sNode1 = zTree_permission_workgroup_index.getSelectedNode();
                alertMsg.confirm("您确定要删除吗？", {
                    okCall: function () {
                        $.post("/Permission/Workgroup/DelWorkgroup/" + sNode1.Id, null, delworkgroup_permission_workgroup_index, "json");
                    }
                });
            } else {
                alertMsg.error("请先选择要删除的工作组！");
                return false;
            }
        });
        $("#removeStaff_permission_workgroup_index").click(function () {
            var rowId = $("#list_permission_workgroup_index").jqGrid("getGridParam", "selrow");
            var selectedNode = zTree_permission_workgroup_index.getSelectedNode();
            if (rowId != null && selectedNode.Id != 0) {
                var staffId = $("#list_permission_workgroup_index").getCell(parseInt(rowId), "Id").toString();
                $.post("/Permission/Workgroup/RemoveFromWorkgroup", { workgroupId: selectedNode.Id, staffId: staffId }, StaffRemoved_Permission_Workgroup, "json");
            }
            else {
                alertMsg.error("请选择职员！");
            }
        });

        InitGrid_Permission_Workgroup();

    });


 
</script>
<div class="page">
                @*顶部按钮*@
                <div class="panelBar">
                <ul class="toolBar" id="ul_permission_workgroup_index">
                            <li>@Html.PermissionButton("Permission.Workgroup.CreateWorkgroup", "addWorkgroup_permission_workgroup_index", "add", "/Permission/Workgroup/CreateWorkgroup/{sid_user}", "new_addWorkgroup", "dialog", "", "true", "添加工作组")</li>
                            <li>@Html.PermissionButton("Permission.Workgroup.EditWorkgroup", "editWorkgroup_permission_workgroup_index", "edit", "/Permission/Workgroup/EditWorkgroup/{sid_user}", "new_editWorkgroup", "dialog", "", "true", "编辑工作组")</li>
                            <li>@Html.PermissionButton("Permission.Workgroup.Delete", "del_permission_workgroup_index", "delete", "javascript:void(0);", "","","","","删除工作组")</li>
                            <li class="line">line</li>
                            <li>@Html.PermissionButton("Permission.Workgroup.AddStaff", "addStaff_permission_workgroup_index", "add", "", "new_addWrokgroup", "dialog", "", "true", "280", "350", "加入职员")</li>
                            <li>@Html.PermissionButton("Permission.Workgroup.RemoveStaff", "removeStaff_permission_workgroup_index", "delete", "javascript:void(0);", "", "", "", "", "移除职员")</li>
		                </ul>

                </div>
                <div id="mainContent_permission_workgroup_index" >
                <div id="LeftPane" class="layoutLeft"  layoutH="28">
                @*菜单树*@
                <ul id="treeWorkgroup_permission_workgroup_index" class="ztree"></ul>
            </div>
            <div id="RightPane_permission_workgroup_index"  class="layoutRight"  layoutH="28">
                    @*表单内容*@
                        <table id="list_permission_workgroup_index">
                        </table>
                        <div id="pager_permission_workgroup_index">
                        </div>
            </div>
             </div>       
</div>
